<template lang="pug">
    CuModal(:visible.sync="visible" no-padding :z-index="10240" top="10%")
        view(class="content" slot="title")
            text(class="text-black") 分享
            text(class="text-black text-bold") {{task.task_name}}
            text(class="text-black") 设置
        view(slot="action")
              button(v-if="!generate_success" class="cu-btn bg-green margin-left" @tap="generate_share_link" id="share-task") 生成
              button(v-else class="cu-btn bg-green margin-left" @tap="close_modal" open-type="share" id="share-task") 分享给好友

        template(v-if="!generate_success")
            form()
                view(class="cu-form-group margin-top")
                    view(class="title") 有效期
                radio-group(class="block" @change="expires_after_change")
                    view(class="cu-form-group")
                        view(class="title") 7天后失效
                        radio(:class="{checked: shareLink.expires_after==7 }" :checked="shareLink.expires_after==7" :value="7")
                    view(class="cu-form-group")
                        view(class="title") 永久有效
                        radio(:class="{checked: shareLink.expires_after==0}" :checked="shareLink.expires_after==0" :value="0")
                view(class="cu-form-group margin-bottom")
                    view(class="title") 可编辑
                    switch(class="green radius" :class="{checked: shareLink.checked}" checked="{checked: shareLink.checked}")
          
        template(v-else)
            view(class="margin-left margin-top margin-bottom text-center text-xl")
                text(class="text-black text-bold") 生成成功
</template>
<script>
/**
 * 分享待办给朋友
 */

import BaseMinx from  "@/mixins/BaseMixin"

export default {
  name: "task-share",

  mixins: [BaseMinx],

  data() {
    return {
      shareLink: {
        expires_after: 7,
        editable: true
      },

      generate_success: false,
    };
  },

  props: {
    visible: { type: Boolean, default: true },

    task: { type: Object, required: true } // 需要把待办数据传进来
  },

  computed: {
    share_link() {
      return Object.assign({ task_id: this.task.id }, this.shareLink);
    }
  },

  methods: {
    generate_share_link() {
      this.post("/share_link/", this.share_link).then(res => {
        if(res.success)
            this.generate_success = true
      });
    },

    expires_after_change({detail}){
        this.shareLink.expires_after = detail.value;
    },

    close_modal(){
        this.$emit('update:visible', false)
        this.$emit('share-btn-click') // 触发分享按钮被点击事件
    }
  }
};
</script>